var currentMedia, currentImage;
var root, mediaElement, mediaImage, mediaControls, mediaTime, mediaScale, mediaPlayback, playerHost;
var progressRect, downloadRect, playHead;
var pauseIcon, playIcon, playGlow, stopBgd, muteBgd, mutedIcon, audio1Icon, audio2Icon, rewindGlow, fforwardGlow, resizeBgd;
var fadeInGlow, fadeOutGlow;
var trackWidth = 290;
var speedModifier = 0;

function root_Loaded(sender, args) { 
     root = sender.findname("root");
     playerHost = document.getElementById("wpfePlayerHost");
    
     mediaImage = sender.findname("mediaImage");
     mediaImage.SetValue("Source", currentImage); 
    
     mediaElement = sender.findname("mediaElement");
     mediaElement.setvalue("MediaOpened", "javascript:OnMediaOpened");
     
     mediaScale = sender.findname("mediaScale");
     mediaPlayback = sender.findname("mediaPlayback");
     playbackScale = sender.findname("playbackScale");
     mediaTime = sender.findname("mediaTime");
     mediaControls = sender.findname("mediaControls");
     playHead = sender.findname("playHead");

     pauseIcon = sender.FindName("pauseIcon");
     playIcon = sender.FindName("playIcon");    
     fadeInGlow = sender.FindName("fadeInGlow"); 
     fadeOutGlow = sender.FindName("fadeOutGlow"); 
     playGlow = sender.FindName("playGlow");    
     rewindGlow = sender.FindName("rewindGlow");
     fforwardGlow = sender.FindName("fforwardGlow");
     stopBgd = sender.FindName("stopBgd");
     resizeBgd = sender.FindName("resizeBgd");
     muteBgd = sender.FindName("muteBgd");
     audio1Icon = sender.FindName("audio1Icon");
     audio2Icon = sender.FindName("audio2Icon");
     mutedIcon = sender.FindName("mutedIcon");            
}

function OnMediaOpened(sender, args) {
    var timer = sender.FindName("timer");
    timer.Begin(); 
}

// Handles the timer's tick event
function timerCompleted (sender, eventArgs) {
     var seconds;

     if (progressRect == null) {
          playHead = mediaElement.FindName("playHead");
          progressRect = mediaElement.FindName("progressRect");
          downloadRect = mediaElement.FindName("downloadRect");
     }

     var pos = mediaElement.GetValue("Position");    
     var duration = mediaElement.GetValue("NaturalDuration").GetValue("Seconds");
     
     if(pos)          
     {
          seconds = pos.GetValue("Seconds");
     
          if(playState == "paused" && speedModifier != 0)
          {
               if(seconds + speedModifier < 0)
                    seconds = 0;
               else if(seconds + speedModifier > duration)
                    seconds = duration;               
               else
                    seconds += speedModifier;
               pos.seconds = seconds;               
               mediaElement.position = pos;
          }     

          seconds = seconds * trackWidth / duration;
          progressRect.SetValue("Width", seconds);
          if(seconds - 10 < 6)
               playHead.SetValue("Canvas.Left", 6);
          else
               playHead.SetValue("Canvas.Left", seconds - 10);
     }    
       

     var dlp = mediaElement.GetValue("DownloadProgress");    
     if(dlp)
     {
          downloadRect.SetValue("Width", trackWidth * dlp);
     }    
     mediaTime.SetValue("Text", secondsToMediaTime(Math.floor(seconds)));    

     // Restart timer
     var timer = sender.FindName("timer");
     timer.Stop();
     timer.Begin();     
}

function secondsToMediaTime(seconds)
{
	var mediaTime = '';
	var hours = Math.floor(seconds / 3600);
	var minutes = Math.floor(seconds / 60);
	
	if (hours > 0)
	{
		mediaTime += hours + ':';
		seconds = seconds - (hours * 3600);
	}	
		
	if (minutes > 0)
	{
		mediaTime += minutes + ":";
		seconds = seconds - (minutes * 60);
	}
	else
		mediaTime += "0:";
	
	if (seconds >= 10)
		mediaTime += seconds;
	else
		mediaTime += "0" + seconds;
	
	return mediaTime;
}
